/*
 * Copyright (c) Tan Menglong <TanMenglong@gmail.com> <+86-15972108715>
 * All rights reserved.
 *
 * This file is part of the RabbitOS.  The RabbitOS is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the Rabbit OS team.
 * 
 * The RabbitOS is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the RabbitOS; see the file COPYING. If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 *
 */
/*
 * Functions to operate descriptor tables.
 * 03-May-08
 */

#include <sys/types.h>
#include <init/dt.h>

gdt_entry_t gdt_entries[7];
gdt_ptr_t gdt_ptr;
idt_entry_t idt_entries[256];
idt_ptr_t idt_ptr;

extern void init_8259(void);

void set_gdt_gate(s32int num, u32int base, u32int limit,
				  u8int access, u8int gran)
{

	gdt_entries[num].base_low = (base & 0xFFFF);
	gdt_entries[num].base_middle = (base >> 16) & 0xFF;
	gdt_entries[num].base_high = (base >> 24) & 0xFF;

	gdt_entries[num].limit_low = (limit & 0xFFFF);
	gdt_entries[num].granularity = (limit >> 16) & 0x0F;

	gdt_entries[num].granularity |= gran & 0xF0;
	gdt_entries[num].access = access;
}

void init_gdt(void)
{
	gdt_ptr.limit = (sizeof(gdt_entry_t) * 7) - 1;
	gdt_ptr.base = (u32int) & gdt_entries;

	// Null segment
	set_gdt_gate(0, 0, 0, 0, 0);
	// Ring 0 Code segment for Kernel
	set_gdt_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
	// Ring 0 Data segment for kernel
	set_gdt_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
	// Ring 1 code segment for servers
	set_gdt_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
	// Ring 1 data segment for servers
	set_gdt_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
	// Ring 3 code segment for user
	set_gdt_gate(5, 0, 0xFFFFFFFF, 0xFA, 0xCF);
	// Ring 3 data segment for user
	set_gdt_gate(6, 0, 0xFFFFFFFF, 0xF2, 0xCF);

	flush_gdt((u32int) & gdt_ptr);
}

static void idt_set_gate(u8int num, u32int base, u16int sel, u8int flags)
{
	idt_entries[num].base_lo = base & 0xFFFF;
	idt_entries[num].base_hi = (base >> 16) & 0xFFFF;

	idt_entries[num].sel = sel;
	idt_entries[num].always0 = 0;
	// We must uncomment the OR below when we get to using user-mode.
	// It sets the interrupt gate's privilege level to 3.
	idt_entries[num].flags = flags /* | 0x60 */ ;
}

void init_idt(void)
{
	idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1;
	idt_ptr.base = (u32int) & idt_entries;
	init_8259();
	memset(&idt_entries, 0, sizeof(idt_entry_t) * 256);

	idt_set_gate(0, (u32int) isr0, 0x08, 0x8E);
	idt_set_gate(1, (u32int) isr1, 0x08, 0x8E);
	idt_set_gate(2, (u32int) isr2, 0x08, 0x8E);
	idt_set_gate(3, (u32int) isr3, 0x08, 0x8E);
	idt_set_gate(4, (u32int) isr4, 0x08, 0x8E);
	idt_set_gate(5, (u32int) isr5, 0x08, 0x8E);
	idt_set_gate(6, (u32int) isr6, 0x08, 0x8E);
	idt_set_gate(7, (u32int) isr7, 0x08, 0x8E);
	idt_set_gate(8, (u32int) isr8, 0x08, 0x8E);
	idt_set_gate(9, (u32int) isr9, 0x08, 0x8E);
	idt_set_gate(10, (u32int) isr10, 0x08, 0x8E);
	idt_set_gate(11, (u32int) isr11, 0x08, 0x8E);
	idt_set_gate(12, (u32int) isr12, 0x08, 0x8E);
	idt_set_gate(13, (u32int) isr13, 0x08, 0x8E);
	idt_set_gate(14, (u32int) isr14, 0x08, 0x8E);
	idt_set_gate(15, (u32int) isr15, 0x08, 0x8E);
	idt_set_gate(16, (u32int) isr16, 0x08, 0x8E);
	idt_set_gate(17, (u32int) isr17, 0x08, 0x8E);
	idt_set_gate(18, (u32int) isr18, 0x08, 0x8E);
	idt_set_gate(19, (u32int) isr19, 0x08, 0x8E);
	idt_set_gate(20, (u32int) isr20, 0x08, 0x8E);
	idt_set_gate(21, (u32int) isr21, 0x08, 0x8E);
	idt_set_gate(22, (u32int) isr22, 0x08, 0x8E);
	idt_set_gate(23, (u32int) isr23, 0x08, 0x8E);
	idt_set_gate(24, (u32int) isr24, 0x08, 0x8E);
	idt_set_gate(25, (u32int) isr25, 0x08, 0x8E);
	idt_set_gate(26, (u32int) isr26, 0x08, 0x8E);
	idt_set_gate(27, (u32int) isr27, 0x08, 0x8E);
	idt_set_gate(28, (u32int) isr28, 0x08, 0x8E);
	idt_set_gate(29, (u32int) isr29, 0x08, 0x8E);
	idt_set_gate(30, (u32int) isr30, 0x08, 0x8E);
	idt_set_gate(31, (u32int) isr31, 0x08, 0x8E);
	
	// don't forget to handle irq - liuw
	idt_set_gate(32, (u32int) irq0, 0x08, 0x8E);
	idt_set_gate(33, (u32int) irq1, 0x08, 0x8E);
	idt_set_gate(34, (u32int) irq2, 0x08, 0x8E);
	idt_set_gate(35, (u32int) irq3, 0x08, 0x8E);
	idt_set_gate(36, (u32int) irq4, 0x08, 0x8E);
	idt_set_gate(37, (u32int) irq5, 0x08, 0x8E);
	idt_set_gate(38, (u32int) irq6, 0x08, 0x8E);
	idt_set_gate(39, (u32int) irq7, 0x08, 0x8E);

	idt_set_gate(40, (u32int) irq8, 0x08, 0x8E);
	idt_set_gate(41, (u32int) irq9, 0x08, 0x8E);
	idt_set_gate(42, (u32int) irq10, 0x08, 0x8E);
	idt_set_gate(43, (u32int) irq11, 0x08, 0x8E);
	idt_set_gate(44, (u32int) irq12, 0x08, 0x8E);
	idt_set_gate(45, (u32int) irq13, 0x08, 0x8E);
	idt_set_gate(46, (u32int) irq14, 0x08, 0x8E);
	idt_set_gate(47, (u32int) irq15, 0x08, 0x8E);

	flush_idt((u32int) & idt_ptr);
}

// initialises the GDT and IDT.
void init_dt()
{
	// Initialise the global descriptor table.
	init_gdt();
	// Initialise the interrupt descriptor table.
	init_idt();
	// Nullify all the interrupt handlers.
	memset(&interrupt_handlers, 0, sizeof(isr_t) * 256);
}
